from bs4 import BeautifulSoup
# 安装BeautifulSoup
# pip install bs4
html = """
<html>
    <body>
        <p class="story">
            Once upon a time there were three little sisters; and their names were
            <a href="http://example.com/elsie" class="sister" id="link1">
                <span>Elsie</span>
            </a>
            Hello
            <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> 
            and
            <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>
            and they lived at the bottom of a well.
        </p>
"""

soup = BeautifulSoup(html, 'lxml')
# 使用prettify方法，把解析的字符以标准缩进格式输出
# print(soup.prettify())

#关联选择(子节点、子孙节点)
# print(soup.p.contents)

# 得到所有子孙节点descendants属性
# print(soup.p.descendants)
# for i, child in enumerate(soup.p.descendants):
#     print(i,child)

#父节点和祖先节点
print(soup.a.parent)
# 父节点的祖先节点
# print (list(enumerate(soup.a.parents)))
# 兄弟节点
# 下一个兄弟元素
print("next_sibling：", soup.a.next_sibling)
# 上一个兄弟元素
print("previous_sibling：", soup.a.previous_sibling)
